﻿using mvcForum.Core.Interfaces;
using mvcForum.Core.Interfaces.DependencyManagement;
using mvcForum.Core.Interfaces.Search;
using System;

namespace mvcForum.Core {

	public class ForumConfiguration {
		public readonly IDependencyContainer container;

		protected ForumConfiguration() { }

		//private ForumConfiguration Configure() {
		//    MVCForumSectionGroup config = MVCForumSectionGroup.Get(ConfigurationManager.OpenExeConfiguration(""));
		//    IDependencyContainer container = this.CreateContainer(config);

		//    this.CreateProvider(config.DatabaseComponent.Type, container);

		//    this.Bind(config.EventListenerComponents, container, typeof(IEventListener));
		//    this.Bind(config.ContentParserComponents, container, typeof(IContentParser));


		//    return this;
		//}

		//private Type GetType(String assembly, String type) {
		//    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		//    Assembly ass = assemblies.Where(a => a.GetName().Name == assembly).FirstOrDefault();
		//    if (ass == null) {
		//        throw new ApplicationException("meh");
		//    }
		//    Type t = ass.GetTypes().Where(ty => ty.FullName == type).FirstOrDefault();
		//    if (t == null) {
		//        throw new ApplicationException("meh2");
		//    }

		//    return t;
		//}

		//private void CreateProvider(String typeString, IDependencyContainer container) {
		//    String assembly = String.Empty;
		//    String type = String.Empty;
		//    this.GetAssemblyAndType(typeString, out assembly, out type);

		//    Type t = this.GetType(assembly, type);
		//    IDependencyBuilder builder = (IDependencyBuilder)Activator.CreateInstance(t, false);
		//    builder.Configure(container);
		//}

		//private IDependencyContainer CreateContainer(MVCForumSection config) {
		//    String assembly = String.Empty;
		//    String type = String.Empty;
		//    this.GetAssemblyAndType(config.DependencyContainerBuilder.Type, out assembly, out type);

		//    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		//    Assembly ass = assemblies.Where(a => a.GetName().Name == assembly).FirstOrDefault();
		//    if (ass == null) {
		//        throw new ApplicationException("meh");
		//    }
		//    Type t = ass.GetTypes().Where(ty => ty.FullName == type).FirstOrDefault();
		//    if (t == null) {
		//        throw new ApplicationException("meh2");
		//    }

		//    IDependencyContainer container = (IDependencyContainer)Activator.CreateInstance(t, false);
		//    container.Configure();

		//    return container;
		//}

		//private void GetAssemblyAndType(String input, out String assembly, out String type) {
		//    String[] parts = input.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
		//    assembly = parts[1].Trim();
		//    type = parts[0].Trim();
		//}

		public ForumConfiguration SetAttachmentConfiguration(IAttachmentStorage attachmentStorage) {
			// Make sure it's unique!!!
			this.container.UnRegister<IAttachmentStorage>();
			this.container.RegisterSingleton<IAttachmentStorage>(attachmentStorage);
			return this;
		}

		public ForumConfiguration AddIndexer(IIndexer indexer) {
			this.container.RegisterSingleton<IIndexer>(indexer);
			return this;
		}

		public ForumConfiguration AddSearcher(ISearcher searcher) {
			this.container.RegisterSingleton<ISearcher>(searcher);
			return this;
		}

		private static Boolean initialized = false;
		private static Object objectLock = new Object();
		public static ForumConfiguration Initialize() {
			if (!initialized) {
				lock (objectLock) {
					if (!initialized) {
						ForumConfiguration config = new ForumConfiguration();
						return config;//.Configure();
					}
				}
			}
			throw new ApplicationException("Already initialized");
		}
	}
}